/****************************************************************
 * Homework 6 Main program
 * Author: Andrew Shore, Wes Ring, JC Anderson, Colin Edwards
 * Date last modified: 5 April 2010
 *
 * This main program drives a simplified simulation of process
 * management in an operating system. First the name of the file
 * that contains the list of processes and the number of timesteps
 * is read from the command line. Note if the number of timesteps 
 * read is -1 then the simulation is run for the maximum long value,
 * so essentially until all the processes run out. An input stream
 * is opened for the file containing the processes, then the file stream
 * and the number of timesteps are passed to the runSimulation function.
 * Finally the file streams are closed and the program ends.
 *
 * Input: none
 *
 * Output: only a log of what takes place
**/
#include "Main.h"

static const char TAG[] = "Main: ";

int main(int argc, char *argv[])
{
  cout << TAG << "Beginning execution" << endl; 

  ifstream inStream;
  Scheduler theScheduler;
  long maxTimeSteps;
  string s;
  int n;

  Utils::CheckArgs(2, argc, argv, "input file, number of timesteps");

  Utils::FileOpen(inStream, argv[1]);
  
  cout << "Take processes from file '" << argv[1] << "'\n";
  
  //these lines read the number of timesteps from the command line
  //then casts it to a int using atoi then a long using static_cast
  s = (argv[2]);
  n = atoi(s.c_str());
  maxTimeSteps = static_cast<long>(n);
  

  if( maxTimeSteps == -1){
    maxTimeSteps = numeric_limits<long>::max();
    
  }
  cout << "Run simulation until time " << maxTimeSteps << endl;
  theScheduler.runSimulation(inStream, maxTimeSteps);
 
  Utils::FileClose(inStream);
  
  cout << TAG << "Ending execution" << endl;
  return 0;
}
